package chat

import (
	"reflect"
	"sanguo/cluster"
	codecs "sanguo/codec/cs"
	"sanguo/node/common/com_type"
	"sanguo/node/common/wordsFilter"
	"sanguo/node/node_chat"
	cs_msg "sanguo/protocol/cs/message"
	ss_meg "sanguo/protocol/ss/message"

	"github.com/golang/protobuf/proto"
	"github.com/sniperHW/kendynet"
)

func onChat(session kendynet.StreamSession, msg proto.Message) {
	req := msg.(*ss_meg.Chat)

	role := chatMgr.Roles[req.GetRoleID()]
	if role == nil {
		node_chat.Infoln("Role is nil", req.GetRoleID())
		return
	}

	chatMgr.PostTask(func() {
		node_chat.Infoln("Chat Msg", req)
		content := wordsFilter.Replace(req.GetContent())

		resp := &cs_msg.ChatToC{
			Content:    proto.String(content),
			Type:       proto.Int32(req.GetType()),
			FromRoleID: proto.Uint64(role.GetRoleID()),
			Name:       proto.String(role.Name),
			Level:      proto.Int32(role.Level),
			Portrait:   proto.Int32(role.Portrait),
		}

		if req.GetType() == com_type.Chat_World {
			for _, r := range chatMgr.Roles {
				r.SendToClient(resp)
				node_chat.Infoln(" WorldChat message send to :", r.GetRoleID())
			}

		} else if req.GetType() == com_type.Chat_Map {
			if role.GetMapChat() == nil {
				node_chat.Debugln(role.GetRoleID(), "MapChat is nil")
				return
			}

			for _, r := range role.GetMapChat().Roles {
				r.SendToClient(resp)
				node_chat.Infoln("MapChat message send to :", r.GetRoleID())
			}
		} else if req.GetType() == com_type.Chat_Team {
			if role.GetTeamChat() == nil {
				node_chat.Debugln(role.GetRoleID(), "TeamChat is nil")
				return
			}

			for _, r := range role.GetTeamChat().Roles {
				r.SendToClient(resp)
				node_chat.Infoln("TeamChat message send to :", r.GetRoleID())
			}

		} else if req.GetType() == com_type.Chat_Player {
			toRole := chatMgr.Roles[req.GetToRoleID()]
			if toRole == nil {
				node_chat.Debugln("PlayerChat %d is nil", req.GetToRoleID())
				return
			}

			role.SendToClient(resp)
			toRole.SendToClient(resp)
		}

	})

}

func onSystemMsg(session kendynet.StreamSession, msg proto.Message) {
	req := msg.(*ss_meg.SystemMsg)
	resp := &cs_msg.ChatToC{
		Content: proto.String(req.GetContent()),
		Type:    proto.Int32(com_type.Chat_System),
	}
	for _, r := range chatMgr.Roles {
		r.SendToClient(resp)
	}
}

func (this *Role) SendToClient(msg proto.Message) {
	relayMsg := &ss_meg.Relay{
		Users: make([]string, 1)[0:0],
	}
	relayMsg.Users = append(relayMsg.Users, this.UserID)
	relayMsg.MsgType = proto.String(reflect.TypeOf(msg).String())
	bytes, err := encoder.EnCode(codecs.NewMessage(0, msg))
	if nil == err {
		//copy(relayMsg.Msg, bytes.Bytes())
		relayMsg.Msg = bytes.Bytes()
		cluster.PostMessage(this.Game, relayMsg)
	} else {
		node_chat.Errorln(err, relayMsg.GetMsgType())
	}
}

func init() {
	cluster.Register(&ss_meg.Chat{}, func(session kendynet.StreamSession, msg proto.Message) {
		//fmt.Println("sssssssssssssss", reflect.TypeOf(msg).String())
		onChat(session, msg)
	})

	cluster.Register(&ss_meg.SystemMsg{}, func(session kendynet.StreamSession, msg proto.Message) {
		onSystemMsg(session, msg)
	})

	//	go func() {
	//		for {
	//			time.Sleep(time.Second * 60)
	//			content := "这是一条来自系统的消息"
	//			resp := &cs_msg.ChatToC{
	//				Content: proto.String(content),
	//				Type:    proto.Int32(com_type.Chat_System),
	//			}
	//			for _, r := range chatMgr.Roles {
	//				r.SendToClient(resp)
	//			}
	//		}
	//	}()
}
